package com.chenlonghai.sbmybatis.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.chenlonghai.sbmybatis.entity.Banner;
import com.chenlonghai.sbmybatis.entity.Cases;
import com.chenlonghai.sbmybatis.entity.ResultModel;
import com.chenlonghai.sbmybatis.service.HoldingsService;

@RestController
@RequestMapping("/cases")
public class HoldingsController {
	@Autowired
	private HoldingsService holdingsService;
	
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public Map<String, Object> listArea(){
		Map<String, Object>modelMap = new HashMap<>();
	
		List<Cases>list = holdingsService.queryCases();
		ResultModel<List<Cases>>result =new ResultModel<List<Cases>>(list, modelMap);
		modelMap = result.getResltMap();
		return modelMap;
	}
	
	@RequestMapping(value="/getcasebyid",method = RequestMethod.GET)
	public Map<String, Object> getAreaById(Integer id){
		System.out.println("sss进来了吗");
		Map<String, Object>modelMap = new HashMap<>();
		
		Cases cases = holdingsService.queryCasesBy(id);
		ResultModel<Cases>result =new ResultModel<Cases>(cases, modelMap);
		modelMap = result.getResltMap();
		return modelMap;
	}

	
	@RequestMapping(value="/addcase",method = RequestMethod.POST)
	public Map<String, Object> addArea(@RequestBody Cases cases){
      	System.out.println("sss进来了吗");
		Map<String, Object>modelMap = new HashMap<>();
		
		modelMap.put("success", holdingsService.insertCase(cases));
		return modelMap;
	}
	@Value("${cbs.location}")
    private String locationPath;
	
	
	@RequestMapping(value="/add",method = RequestMethod.POST,produces="application/json;charset=UTF-8")
	public Map<String, Object> addBanner(
            HttpServletRequest request, Cases cases){
		

		
		
		Map<String, Object>modelMap = new HashMap<>();
        System.out.println(cases.getImgUrl());
        System.out.println(cases.getLinkUrl());
        System.out.println(cases.getType());
        System.out.println(cases.getTittle());
    
        cases.setCreateTime(new Date());
        cases.setUpdateTime(new Date());
   	     boolean  isOk =  holdingsService.insertCase(cases);
        if (isOk) {
        	ResultModel<String>result = new ResultModel<String>("",modelMap);
        	 return  result.getResltMap();
		}
     	ResultModel<String>result = new ResultModel<String>(null,modelMap);
     	
        return result.getResltMap();


	}
	
	@RequestMapping(value="/update",method = RequestMethod.POST,produces="application/json;charset=UTF-8")
	public Map<String, Object> updateBanner(
            HttpServletRequest request, Cases cases){
		

		
		
		Map<String, Object>modelMap = new HashMap<>();
        System.out.println(cases.getImgUrl());
        System.out.println(cases.getLinkUrl());
        System.out.println(cases.getType());
        System.out.println(cases.getTittle());
        cases.setUpdateTime(new Date());
   	     boolean  isOk =  holdingsService.updateCase(cases);
        if (isOk) {
        	ResultModel<String>result = new ResultModel<String>("",modelMap);
        	 return  result.getResltMap();
		}
     	ResultModel<String>result = new ResultModel<String>(null,modelMap);
     	
        return result.getResltMap();


	}
	
    @RequestMapping(value="/addcases", method = RequestMethod.POST)
    public @ResponseBody String uploadImg(@RequestParam("file") MultipartFile file,
            HttpServletRequest request, Cases cases) {
    	
    	System.out.println(cases.getTittle());
    	System.out.println(cases.getContent());
    	System.out.println(cases.getTittle());

        if (!file.isEmpty()) {
        	

 		    SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
            String saveFileName = df.format(new Date()) + file.getOriginalFilename();
            File saveFile = new File(locationPath+"case/" + saveFileName);
            
        	System.out.println(locationPath+"case/" + saveFileName);
            if (!saveFile.getParentFile().exists()) {
                saveFile.getParentFile().mkdirs();
            }
            try {
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(saveFile));
                out.write(file.getBytes());
                out.flush();
                out.close();
                
            	cases.setImgUrl("/images/"+"case/" +saveFileName);
                cases.setCreateTime(new Date());
            	cases.setUpdateTime(new Date());
            	
            	 boolean  isOk =  holdingsService.insertCase(cases);
             	System.out.println(isOk + cases.getImgUrl());
            	if(isOk==true){
            		 return " 上传成功";
            	}
                
                
                return  " 上传失败";
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            }
        } else {
            return "上传失败，因为文件为空.";

    }
    }
	
	
	@RequestMapping(value="/updatecase",method = RequestMethod.POST)
	public Map<String, Object> updateArea(@RequestBody Cases cases){
		Map<String, Object>modelMap = new HashMap<>();
		modelMap.put("success", holdingsService.updateCase(cases));
		return modelMap;
	}
	
	@RequestMapping(value="/deletecase",method = RequestMethod.POST,produces="application/json;charset=UTF-8")
	public Map<String, Object> deletecase(Integer id){
		Map<String, Object>modelMap = new HashMap<>();
		
		System.out.println("case-->"+id);
		 boolean isOk = holdingsService.deleteCase(id);
		 if (isOk) {
				ResultModel<String>result =new ResultModel<String>("", modelMap);
				modelMap = result.getResltMap();
				return modelMap;	
		}
		 ResultModel<String>result =new ResultModel<String>(null, modelMap);
		modelMap = result.getResltMap();
		return modelMap;
	}
}
